package com.huanwei.login_regist;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.huanwei.cc.R;
import com.huanwei.cc.db.Z_Login_UserInfoDao;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 注册页面
 */
public class RegistActivity extends AppCompatActivity {

    @BindView(R.id.img_back)
    RelativeLayout imgBack;
    @BindView(R.id.lr_sr_sjh)
    EditText etPhone;
    @BindView(R.id.lr_sr_mm)
    EditText etMm;
    @BindView(R.id.lr_cxsr_mm)
    EditText etQrmm;
    @BindView(R.id.but_regist)
    Button butRegist;
    Z_Login_UserInfoDao z_userInfoDao;
    @BindView(R.id.name)
    EditText name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_regist);
        z_userInfoDao = new Z_Login_UserInfoDao(this);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.img_back, R.id.but_regist})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.but_regist:
                if (TextUtils.isEmpty(name.getText().toString())) {
                    Toast.makeText(this, "请输入姓名", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(etPhone.getText().toString())) {
                    Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(etMm.getText().toString())) {
                    Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (etMm.getText().toString().length() < 6 || etQrmm.getText().toString().length() < 6) {
                    Toast.makeText(this, "密码太短，请重新输入！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!etMm.getText().toString().equals(etQrmm.getText().toString())) {
                    Toast.makeText(this, "两次输入的密码不同，请重新输入！", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (z_userInfoDao.shifoucunzai(etPhone.getText().toString())) {
                    Toast.makeText(this, "账号已存在", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    z_userInfoDao.addUser(name.getText().toString(),etPhone.getText().toString(), etMm.getText().toString());
                    Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
                    finish();
                }
                break;
        }
    }
}
